﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JChatCore.CounTry;
using JChatData.CountryMap;
using JChatData.Helper;

namespace JChatSerVices.CountryService
{
    public partial class CountryService:ICountryService
    {
        DBConectString _db;
        // ChatModel _context;
        GetConnect.GetFileConnect _get;
        public CountryService()
        {
            _get = new GetConnect.GetFileConnect();
            string file = _get.GetFile("conn");
            _db = new DBConectString(file);
         
        }
        #region Get Data
        public IQueryable<countryproperties> GetAllCountry()
        {
            //var _country = (from c in _db._context.JChat_CounTry
            //                select c).ToList();
            var _c = (from v in _db._context.JChat_CounTry
                     select new countryproperties { CountryID = v.CountryID, NameCountry = v.NameCountry });
            return _c;
        }
        public IQueryable<countryproperties> GetCountryByID(int _id)
        {
            //var _country = (from c in _db._context.JChat_CounTry
            //                where c.CountryID == _id
            //                select c).ToList();
            var _c = (from v in _db._context.JChat_CounTry
                      where v.CountryID == _id
                        select new countryproperties { CountryID = v.CountryID, NameCountry = v.NameCountry });
            return _c;
        }
        #endregion
        #region Add / Update / Delete
        public bool AddCountry(CounTry _country)
        {
            try
            {
                _db._context.JChat_CounTry.AddObject(_country);
                _db._context.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public bool UpdateCountry(int _id, CounTry _countrytable)
        {
            try
            {
                var _country = (from c in _db._context.JChat_CounTry
                                where c.CountryID == _id
                                select c).SingleOrDefault();
                _country.NameCountry = _countrytable.NameCountry;
                _db._context.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public bool DeleteCountry(int _id)
        {
            try
            {
                var _country = (from c in _db._context.JChat_CounTry
                                where c.CountryID == _id
                                select c).SingleOrDefault();
                _db._context.DeleteObject(_country);
                _db._context.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        #endregion
    }
}
